﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class RoomAvailability : Form
    {

        DateTime checkIn;
        DateTime checkOut;
        string roomType;
        DataTable availableRoomTab;
        public RoomAvailability(DateTime checkInDate, DateTime checkOutDate, string SelectedoomType, DataTable roomTab)
        {           
            checkIn = checkInDate;
            checkOut= checkOutDate;
            roomType = SelectedoomType;
            availableRoomTab = roomTab;
            InitializeComponent();  
        }

        #region Page_Load
        private void RoomAvailability_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'roomInformationDataSet.Room' table. You can move, or remove it, as needed.
            this.roomTableAdapter.Fill(this.roomInformationDataSet.Room);
            table_load();
        }
        #endregion Page_Load

        #region table_Load
        DataView roomDV = new DataView();
        NewBooking newBookingChild;
        AllBookingscs allBookingChild;
        void table_load()
        {
            dataGridView1.DataSource = roomDV = availableRoomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.RowHeadersVisible = false;
        }
        #endregion table_Load     

        #region Filters
        private void singleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Type='Single'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }

        private void doubleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Type='Double'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }

        private void deluxeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Type='Deluxe'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }


        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Level='1'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Level='2'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
           
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available' and Room_Level='3'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            DataTable roomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            roomTab.Load(new SqlCommand("Select * from Room where Room_Status='Available'", connection).ExecuteReader());
            dataGridView1.DataSource = roomDV = roomTab.DefaultView;
            dataGridView1.RowTemplate.Height = 50;
        }
        #endregion Filters

        #region continueButton
        BookingDetails bd;
        private void button1_Click(object sender, EventArgs e)
        {
            bool result = false;
            ArrayList selectedRoomList = new ArrayList();
            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                bool IsChecked = Convert.ToBoolean((dataGridView1[0, i] as DataGridViewCheckBoxCell).FormattedValue);
                if (IsChecked == true)
                {
                    Room r = new Room();
                    r.roomUnitNo = Convert.ToInt32(dataGridView1[1, i].Value);
                    r.roomLevel = Convert.ToInt32(dataGridView1[2, i].Value);
                    r.roomType = (string)dataGridView1[3, i].Value;
                    r.roomStatus = (string)dataGridView1[4, i].Value;
                    selectedRoomList.Add(r);
                    result = true;
                }
            }

            if (result == true)
            {
                CloseAllForms();
                if (bd == null)
                {
                    bd = new BookingDetails(selectedRoomList, checkIn, checkOut);
                    bd.MdiParent = this.MdiParent;
                    bd.Location = new Point(0, 0);
                    bd.Show();
                }
            }
            else
            {
                MessageBox.Show("To be continue,Please Select At Least One Room.");
            }

        }
        #endregion ContinueButton

        #region backButton
        private void button2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this.MdiParent;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();
                if (newBookingChild == null)
                {
                    newBookingChild = new NewBooking();
                    newBookingChild.MdiParent = this.MdiParent;
                    newBookingChild.Location = new Point(100, 70);
                    // Display the form.
                    newBookingChild.Show();
                    newBookingChild.Activate();
                }
            }
        }
        #endregion backButton

        #region closeAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this &&form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                bd = null;
                allBookingChild = null;
                newBookingChild = null;
            }
        }
        #endregion CloseAllForms
    }  
}
